package com.erpanderp.wam.web;

import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.WAMUserDAO;

public class WAMUserCredential extends ServerResource {

	@Get("xml")
	public Representation getXML() {
		String userCode = (String) getRequest().getAttributes().get("wu");
		String password = (String) getRequest().getAttributes().get("wupw");
		WAMUser wamUser = new WAMUserDAO().validateWAMUser(userCode, password);
		if(wamUser == null) {
			setStatus(Status.CLIENT_ERROR_NOT_FOUND);
			return null;
		}
		StringBuffer sb = new StringBuffer("<wamuser>");
		sb.append("<employeecode>");
		sb.append(wamUser.getEmployeeCode());
		sb.append("</employeecode>");
		sb.append("<firstname>");
		sb.append(wamUser.getEmployee().getFirstName());
		sb.append("</firstname>");
		sb.append("<lastname>");
		sb.append(wamUser.getEmployee().getLastName());
		sb.append("</lastname>");
		sb.append("<designation>");
		sb.append(wamUser.getEmployee().getDesignation());
		sb.append("</designation>");
		sb.append("<module>");
		sb.append(wamUser.getEmployee().getModuleName());
		sb.append("</module>");
		sb.append("<email>");
		sb.append(wamUser.getEmployee().getEmail());
		sb.append("</email>");				
		sb.append("</wamuser>");
		return new StringRepresentation(sb, MediaType.APPLICATION_XML);
	}

}